package com.arvin.controller;

import com.arvin.pojo.Link;
import com.arvin.pojo.Log;
import com.arvin.pojo.Result;
import com.arvin.service.LinkService;
import com.arvin.service.LogService;
import com.arvin.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin/link")
@Tag(name = "链接接口",description = "链接接口")
@Slf4j
public class LinkController {

    @Autowired
    private LinkService linkService;
    @Autowired
    private LogService logService;

    /**
     * 根据链接主题获取链接信息
     * @param linkName
     * @return
     */
    @Operation(summary = "获取链接信息",description = "获取链接信息")
    @GetMapping("/getInfo")
    public Result<List<Link>> getLinkInfo(String linkName){
        List<Link> linkList = linkService.getLinkInfo(linkName);
        return Result.success(linkList);
    }

    /**
     * 根据id删除链接
     * @param linkId
     * @return
     */
    @Operation(summary = "根据id删除链接",description = "根据id删除链接")
    @DeleteMapping("/deleteById/{linkId}")
    public Result deleteLinkById(@PathVariable Integer linkId){
        Link link = linkService.getLinkById(linkId);
        linkService.deleteLinkById(linkId);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("链接");
        log.setLogOperate("管理员" + username + "删除了链接信息：" + link);
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);
        return Result.success();
    }

    /**
     * 根据id获取链接
     * @param linkId
     * @return
     */
    @Operation(summary = "根据id获取链接",description = "根据id获取链接")
    @GetMapping("/getById/{linkId}")
    public Result<Link> getLinkById(@PathVariable Integer linkId){
        Link link = linkService.getLinkById(linkId);
        return Result.success(link);
    }

    /**
     * 编辑链接
     * @param link
     * @return
     */
    @Operation(summary = "编辑链接",description = "编辑链接")
    @PutMapping("/updateLink")
    public Result updateLink(@RequestBody Link link){
        linkService.updateLink(link);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("链接");
        log.setLogOperate("管理员" + username + "修改了链接信息：" + link);
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 添加链接
     * @param link
     * @return
     */
    @Operation(summary = "添加链接",description = "添加链接")
    @PostMapping("/insertLink")
    public Result addLink(@RequestBody Link link){
        linkService.addLink(link);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("链接");
        log.setLogOperate("管理员" + username + "新增了链接信息：" + link);
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);
        return Result.success();
    }
}
